
package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.example.pages.InternalPage;
import com.example.pages.LoginPage;
import com.example.pages.MyProfile;

import ru.esteru.selenium.factory.WebDriverFactory;


public class changePassword {
  private WebDriver driver;
  private WebDriverWait wait;
  private String baseUrl;
  private StringBuffer verificationErrors = new StringBuffer();
private LoginPage loginPage;
private InternalPage somePage;
private MyProfile myProfile;


  @Before
  public void setUp() throws Exception {
	driver = WebDriverFactory.getDriver(DesiredCapabilities.firefox());
	wait = new WebDriverWait(driver, 10);
	baseUrl = "http://localhost/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testUntitled() {
	  goToMainPage();
	    loginAs("admin", "admin");
	    goToUserMyProfile();
	    changePassword("123", "123");
	    assertTrue(myProfile.thereIsUserManagement());
	    loguot();
	  }



private void changePassword(String password, String passwordAgain) {
	myProfile.passwordField.clear();
	myProfile.passwordField.sendKeys(password);
	myProfile.passwordAgain.clear();
	myProfile.passwordAgain.sendKeys(password2);
}

private void goToUserMyProfile() {
	driver.findElement(By.linkText("My profile")).click();
	myProfile = PageFactory.initElements(driver, MyProfile.class);
	}

private void loguot() {
	 somePage.menuLogoutLink.click();
		wait.until(alertIsPresent()).accept();

}

private void loginAs(String username, String password) {
	loginPage.userNameField.clear();
    loginPage.userNameField.sendKeys(username);
    loginPage.passwordField.clear();
    loginPage.passwordField.sendKeys(password);	
    loginPage.loginButton.click();
}

private void goToMainPage() {
    driver.get(baseUrl + "/php4dvd/");
    loginPage = PageFactory.initElements(driver, LoginPage.class);
    }


  @After
  public void tearDown() throws Exception {
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }
}
  


